package com.amqr.testfragment.fragment.style1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.amqr.testfragment.GeneralConst;
import com.amqr.testfragment.R;

/**
 * User: LJM
 * Date&Time: 2016-11-23 & 21:04
 * Describe: Describe Text
 */
public class SpcOneFragment extends Fragment {
    private View rootView;
    private Toolbar mToolBar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(GeneralConst.TAG,"SpcOneFragment --- onCreateView");
        rootView = View.inflate(getActivity(), R.layout.fragment_spc_one, null);
        initView();
        setHasOptionsMenu(true); // 让Fragment的菜单按钮生效
        return rootView;
    }
    private void initView() {
        rootView.findViewById(R.id.mTvOne).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"SpcOneFragment的Tv点击",Toast.LENGTH_SHORT).show();
            }
        });


        mToolBar = (Toolbar) rootView.findViewById(R.id.mToolBar);
        mToolBar.setTitle("拥抱世界");
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolBar); // 这样切换后Fragment会失效

        mToolBar.setOverflowIcon(getResources().getDrawable(R.drawable.icon_menu)); // 指定菜单按钮图标
        mToolBar.inflateMenu(R.menu.menu_diy);

        //mToolBar.inflateMenu(R.menu.menu_diy);
        mToolBar.setOnMenuItemClickListener(onMenuItemClick); // 菜单按钮点击监听

        mToolBar.setNavigationIcon(R.drawable.arrow_left); // 返回箭头
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

    }

    // 创建关联菜单
    // 下面这里先clean后inflate这么写是为了避免Fragment和Activity同是带有Toolbar,再加重复的东西
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //((AppCompatActivity) getActivity()).getMenuInflater().inflate(R.menu.menu_diy, menu);

        menu.clear();
        inflater.inflate(R.menu.menu_diy, menu);
        super.onCreateOptionsMenu(menu, inflater);


    }

    // 菜单的点击回调
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.item_hello:
                    Toast.makeText(getActivity(),"白衣少年",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item_boy:
                    Toast.makeText(getActivity(),"少年,热闹海岸线",Toast.LENGTH_SHORT).show();
                    break;
            }

            return true;
        }
    };

}
